package cn.iocoder.yudao.module.member.controller.admin.banner;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.banner.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.banner.BannerDO;
import cn.iocoder.yudao.module.member.service.banner.BannerService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - banner图管理")
@RestController
@RequestMapping("/member/banner")
@Validated
public class BannerController {

    @Resource
    private BannerService bannerService;

    @PostMapping("/create")
    @Operation(summary = "创建banner图管理")
    public CommonResult<String> createBanner(@Valid @RequestBody BannerSaveReqVO createReqVO) {
        return success(bannerService.createBanner(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新banner图管理")
    public CommonResult<Boolean> updateBanner(@Valid @RequestBody BannerSaveReqVO updateReqVO) {
        bannerService.updateBanner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除banner图管理")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBanner(@RequestParam("id") String id) {
        bannerService.deleteBanner(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得banner图管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BannerRespVO> getBanner(@RequestParam("id") String id) {
        BannerDO banner = bannerService.getBanner(id);
        return success(BeanUtils.toBean(banner, BannerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得banner图管理分页")
    public CommonResult<PageResult<BannerRespVO>> getBannerPage(@Valid BannerPageReqVO pageReqVO) {
        PageResult<BannerDO> pageResult = bannerService.getBannerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BannerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出banner图管理 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBannerExcel(@Valid BannerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BannerDO> list = bannerService.getBannerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "banner图管理.xls", "数据", BannerRespVO.class,
                        BeanUtils.toBean(list, BannerRespVO.class));
    }

}